package cn.tx.demo;

import java.sql.*;

public class JDBCDemo2 {

    static  final  String username="root";
    static  final  String password="root";
    static  final  String url="jdbc:mysql://127.0.0.1:3306/mydb4";
    static  final  String driver="com.mysql.jdbc.Driver";

    public static void login(String uname,String pword){
        String sql="select * from txuser  t where t.username='"+uname+"' and t.password='"+pword+"'";


        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        try {
            //驱动加载
            Class.forName(driver);
            //建立连接
            conn = DriverManager.getConnection(url, username, password);
            //获得一个执行SQL的对象
            stmt= conn.createStatement();
            //执行SQL
             rs= stmt.executeQuery(sql);
            System.out.println(sql);
            if(rs.next()){
                System.out.println("登陆成功");
            }else{
                System.out.println("用户名或者密码错误");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null){
                    rs.close();
                }
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


    public static void main(String[] args) {

        login("zhangsan","456");
    }
}
